package com.losca.gulimall.auth.control;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.TypeReference;
import com.losca.common.exception.BizCodeEnum;
import com.losca.common.utils.R;
import com.losca.common.vo.MemberResponseVo;
import com.losca.gulimall.auth.feign.MemberFeignService;
import com.losca.gulimall.auth.feign.SmsService;
import com.losca.gulimall.auth.vo.UserLoginVo;
import com.losca.gulimall.auth.vo.UserRegisterVo;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

import static com.losca.common.constant.AuthServerConstant.LOGIN_USER;
import static com.losca.common.constant.AuthServerConstant.SMS_CODE_CACHE_PREFIX;

@Controller
public class LoginController {
    @Resource
    SmsService smsService;
    @Resource
    StringRedisTemplate stringRedisTemplate;
    @Resource
    MemberFeignService memberFeignService;

    //@GetMapping("/login.html")
    // public String login(ModelMap map) {
    //    return "login";
    //}
    //
    //@GetMapping("/reg.html")
    // public String reg(){
    //    return "reg";
    //}
    @PostMapping("/sms/sendCode")
    @ResponseBody
    public R sendCode(@RequestParam("phone") String phone) {
        // 接口防刷
        String redisCode = stringRedisTemplate.opsForValue().get(SMS_CODE_CACHE_PREFIX + phone);
        if (StrUtil.isNotBlank(redisCode)) {
            String s = redisCode.split("_")[1];
            long currentTime = Long.parseLong(s);
            if (System.currentTimeMillis() - currentTime < 60000) {
                return R.error(BizCodeEnum.SMS_CODE_EXCEPTION.getCode(), BizCodeEnum.SMS_CODE_EXCEPTION.getMessage());
            }
        }
        String code = RandomUtil.randomNumbers(5);
        stringRedisTemplate.opsForValue().set(SMS_CODE_CACHE_PREFIX + phone, code + "_" + System.currentTimeMillis(), 10, TimeUnit.MINUTES);
        return smsService.sendCode(phone, code);
    }

    /**
     * TODO: 重定向携带数据：利用session原理，将数据放在session中。
     * TODO:只要跳转到下一个页面取出这个数据以后，session里面的数据就会删掉
     * TODO：分布下session问题
     * RedirectAttributes：重定向也可以保留数据，不会丢失
     * 用户注册
     *
     * @return
     */
    @PostMapping(value = "/register")
    public String register(@Valid UserRegisterVo vos, BindingResult result,
                           RedirectAttributes attributes) {

        // 如果有错误回到注册页面
        if (result.hasErrors()) {
            Map<String, String> errors = result.getFieldErrors().stream().collect(Collectors.toMap(FieldError::getField, FieldError::getDefaultMessage));
            attributes.addFlashAttribute("errors", errors);
            // 校验出错回到注册页面
            return "redirect:http://auth.mall.com/reg.html";
            // return "forward:/reg.html";
            // 不采用转发，转发一刷新会显示是否重新提交表单
            // return "reg";
        }
        // 校验验证码
        String code = vos.getCode();
        // 获取存入redis中的验证码
        String redisCode = stringRedisTemplate.opsForValue().get(SMS_CODE_CACHE_PREFIX + vos.getPhone());
        if (StrUtil.isNotBlank(redisCode)) {
            if (code.equals(redisCode.split("_")[0])) {
                // 删除验证码，令牌机制
                stringRedisTemplate.delete(SMS_CODE_CACHE_PREFIX + vos.getPassword());
                // 验证码通过，真正注册，调用远程服务进行注册
                R register = memberFeignService.register(vos);
                if (register.getCode() == 0) {
                    // 成功
                    return "redirect:http://auth.mall.com/login.html";
                } else {
                    // 失败
                    Map<String, String> errors = new HashMap<>();
                    errors.put("msg", register.getData("msg", new TypeReference<String>() {
                    }));
                    attributes.addFlashAttribute("errors", errors);
                    return "redirect:http://auth.mall.com/reg.html";
                }


            } else {
                // 效验出错回到注册页面
                Map<String, String> errors = new HashMap<>();
                errors.put("code", "验证码错误");
                attributes.addFlashAttribute("errors", errors);
                return "redirect:http://auth.mall.com/reg.html";
            }
        } else {
            // 效验出错回到注册页面
            Map<String, String> errors = new HashMap<>();
            errors.put("code", "验证码错误");
            attributes.addFlashAttribute("errors", errors);
            return "redirect:http://auth.mall.com/reg.html";
        }


    }

    @GetMapping(value = "/login.html")
    public String loginPage(HttpSession session) {

        //从session先取出来用户的信息，判断用户是否已经登录过了
        Object attribute = session.getAttribute(LOGIN_USER);
        //如果用户没登录那就跳转到登录页面
        if (attribute == null) {
            return "login";
        } else {
            return "redirect:http://mall.com";
        }

    }


    @PostMapping(value = "/login")
    public String login(UserLoginVo vo, RedirectAttributes attributes, HttpSession session) {

        //远程登录
        R login = memberFeignService.login(vo);

        if (login.getCode() == 0) {
            MemberResponseVo data = login.getData("data", new TypeReference<MemberResponseVo>() {});
            session.setAttribute(LOGIN_USER,data);
            return "redirect:http://mall.com";
        } else {
            Map<String,String> errors = new HashMap<>();
            errors.put("msg",login.getData("msg",new TypeReference<String>(){}));
            attributes.addFlashAttribute("errors",errors);
            return "redirect:http://auth.mall.com/login.html";
        }
    }


}
